﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

using YouHP.IDAL;
using YouHP.Model;

namespace YouHP.SqlServerDAL
{
    public class ActivityNewsDAL : IActivityNews
    {
        #region basic operations
        public bool AddActivityNews(ActivityNewsInfo info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.ActivityNewsSP_AddActivityNews,
                info.Title,
                info.Introduction,
                info.Body,
                info.StartTime,
                info.EndTime,
                info.Area,
                info.Dimensions,
                info.HasImage,
                info.ImagePath,
                info.Keyword,
                info.Source,
                info.UpdateTime
                ) > 0;
        }

        public bool UpdateActivityNews(ActivityNewsInfo info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.ActivityNewsSP_UpdateActivityNews,
                info.Id,
                info.Title,
                info.Introduction,
                info.Body,
                info.StartTime,
                info.EndTime,
                info.Area,
                info.Dimensions,
                info.HasImage,
                info.ImagePath,
                info.Keyword,
                info.Source,
                info.UpdateTime
                ) > 0;
        }

        public bool DeleteActivityNews(int[] ids)
        {
            int cnt = ids.Length;
            if (cnt <= 0)
                return false;

            StringBuilder sb = new StringBuilder();
            sb.Append("[id] in (");
            sb.Append(ids[0]);

            for (int i = 1; i < cnt; i++)
            {
                sb.Append(", ");
                sb.Append(ids[i]);
            }
            sb.Append(")");

            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.ActivityNewsSP_DelActivityNews,
                sb.ToString()) >= cnt;
        }

        public ActivityNewsInfo GetActivityNewsInfo(int id)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.ActivityNewsSP_GetActivityNews,
                id) as IDataReader;
            return CBO.FillObject<ActivityNewsInfo>(reader);
        }
        #endregion

        #region list and search operations
        private List<ActivityNewsInfo> GetActivityNewsList(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                Common.GetFullName(Common.ActivityNewsSP_GetActivityNewsList),
                pageSize, pageIndex, condStr, orderStr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }
            dr = dset.Tables[1].CreateDataReader();

            return CBO.FillCollection<ActivityNewsInfo>(dr);
        }

        public List<ActivityNewsInfo> GetActivityNewsList(int pageSize, int pageIndex, out int count)
        {
            return GetActivityNewsList(pageSize, pageIndex, "", " [ActivityNews].[Id] desc ", out count);
        }

        public List<ActivityNewsInfo> GetActivityNewsByName(int pageSize, int pageIndex, string keyword, out int count)
        {
            string condStr = string.Format(@" [Title] like '%{0}%' ", keyword);
            return GetActivityNewsList(pageSize, pageIndex, condStr, " [ActivityNews].[Id] desc ", out count);
        }
        #endregion
    }
}

